package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.view.TimeButton;

import java.util.HashMap;
import java.util.Map;

public class ForgetPayPasswordActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back;
    private TextView tvTitle;
    private TextView tvPhoneNumber,tvGetCode;
    private EditText edVerificationCode;
    private Button btnNext;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_paypassword);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        initView();
        setData();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("重置支付密码");
        tvPhoneNumber = serchViewById(R.id.tv_phoneNumber);
        tvGetCode = serchViewById(R.id.get_code);
        tvGetCode.setOnClickListener(this);
        edVerificationCode = serchViewById(R.id.et_verificationCode);
        btnNext = serchViewById(R.id.btn_next);
        btnNext.setEnabled(false);
        btnNext.setOnClickListener(this);
        edVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(edVerificationCode.getText().toString().length()>5){
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.white));
                    btnNext.setBackground(getResources().getDrawable(R.drawable.bg_btn_blue_selector));
                }else {
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.gray));
                    btnNext.setBackground(getResources().getDrawable(R.drawable.bg_btn_next_normal));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void setData(){
        StringBuilder msg = new StringBuilder().append(application.getUserInfo().telNum.substring(0, 3))
                .append("****").append(application.getUserInfo().telNum.substring(7));
        tvPhoneNumber.setText(msg);
        }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.get_code:
                sendCode();
                edVerificationCode.requestFocus();
                break;
            case R.id.btn_next:
                    checkValidationCode();
                break;
        }
    }


    private void sendCode(){
        /**
         * 获取验证码：codeType = 0;
         */
        String codeType = "1";
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("tel",application.getUserInfo().telNum);
        params.put("codeType",codeType);
        showSpotsDialog("正在发送验证码···");
        VRHttp.sendRequest(ForgetPayPasswordActivity.this, HttpLink.GET_VERIFICATIONCODE_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if (response != null) {
                    ResponseBean bean = (ResponseBean) response;
                    if (bean.status.equals("0")) {
                        toastShow("已发送！请查收");
                        TimeButton timeButton = new TimeButton(ForgetPayPasswordActivity.this, 60 * 1000, 1000, tvGetCode);
                        timeButton.start();
                    } else if (bean.status.equals("1")) {
                        toastShow("获取验证码失败！请重试");
                    }
                } else {
                    toastShow("获取验证码失败");
                }
            }
            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("发送验证码失败");
                closeSpotsDialog();
            }
        });
    }

    private void checkValidationCode(){
        /**
         * 验证验证码：codeType = 0;
         */
        String codeType = "1";
        Map<String,String> params = new HashMap<>();
        params.put("type",codeType);
        params.put("code",edVerificationCode.getText().toString());
        showSpotsDialog("正在发送验证码···");
        VRHttp.sendRequest(ForgetPayPasswordActivity.this, HttpLink.CHECK_VERIFICATIONCODE_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if (response != null) {
                    ResponseBean bean = (ResponseBean) response;
                    if (bean.status.equals("0")) {
                        toastShow("验证码正确");
                        Intent intent = new Intent(ForgetPayPasswordActivity.this,SetPayPasswordActivity.class);
                        intent.putExtra("operaType","0");
                        startActivity(intent);
                        finish();
                    } else if (bean.status.equals("1")) {
                        toastShow("验证码不正确！");
                    }else if (bean.status.equals("99")) {
                        toastShow("登录超时请重新登陆后再试！");
                    }
                } else {
                    toastShow("验证时出错出错");
                }
            }
            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("验证出错");
                closeSpotsDialog();
            }
        });
    }
}
